package com.ruoyi.business.controller;

import com.ruoyi.business.service.S7EnergyDataService;
import com.ruoyi.common.annotation.Anonymous;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Random;

@RestController
@RequestMapping("/energy")
public class EnergyTestController {

    @Autowired
    private S7EnergyDataService energyDataService;

    private final Random random = new Random();

    /**
     * 自动生成模拟能源数据并处理
     */
    @Anonymous
    @GetMapping("/simulateAuto")
    public String simulateEnergyDataAuto() {

        // 自动生成随机值
        BigDecimal gas = BigDecimal.valueOf(10 + random.nextDouble() * 10); // 10~20
        BigDecimal waterMain = BigDecimal.valueOf(15 + random.nextDouble() * 10); // 15~25
        BigDecimal waterBypass = BigDecimal.valueOf(5 + random.nextDouble() * 5); // 5~10

        // 调用 service 处理数据
        energyDataService.processPlcData("192.168.0.1", "mildMildfurnactu8", gas);
        energyDataService.processPlcData("192.168.0.1", "waterCoolActualfloww", waterMain);
        energyDataService.processPlcData("192.168.0.1", "waterCoolActualflows", waterBypass);

        return String.format("模拟能源数据已处理: 天然气=%.2f, 主管水=%.2f, 旁通水=%.2f",
                gas, waterMain, waterBypass);
    }
}
